package com.yeyks.wxpay.impl;

import com.alibaba.fastjson.JSON;
import com.yeyks.exception.ServiceException;
import com.yeyks.wxpay.WXJsapiPayService;
import com.yeyks.wxpay.WXPay;
import com.yeyks.exception.ServiceException;
import com.yeyks.wxpay.WXJsapiPayService;
import com.yeyks.wxpay.WXPay;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author fuchangshuai
 * @date 2019/3/19 4:47 PM
 */
@Service
@Slf4j
public class WXJsapiPayServiceImpl implements WXJsapiPayService {

    @Value("${wx.pay.oldNotifyUrl}")
    private String oldNotifyUrl;

    /**
     * JSAPI支付
     *
     * @param orderNo
     * @param totalFee
     * @param ip
     * @return
     */
    @Override
    public String jsApiPay(Integer totalFee, String orderNo, String ip, String openid) {
        try {
            log.info("微信JSAPI支付回调地址{}", oldNotifyUrl);
            MyConfig config = new MyConfig();
            WXPay wxpay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", "正常支付");
            data.put("out_trade_no", orderNo);
            data.put("total_fee", String.valueOf(totalFee));
            data.put("spbill_create_ip", ip);
            data.put("notify_url", oldNotifyUrl);
            data.put("trade_type", "JSAPI");
            data.put("openid", openid);
            Map<String, String> resp = wxpay.unifiedOrder(data);
            log.info("微信JSAPI支付参数{}", resp);
            System.out.println(resp);
            return JSON.toJSONString(resp);
        } catch (Exception e) {
            throw new ServiceException("微信支付异常");
        }
    }
}
